<?php
class ArcControl extends Control {

    //显示文章列表
    function index() {
        C("debug",0);
        $db = K("arc");
        $total = $db->where("huishou=0")->count(); //获得总条数
        $page=new page($total,"10",3,1);
        $data=$db->where("huishou=0")->order("aid desc")->findall($page->limit());
        $page=$page->show();
        $this->assign("page",$page);
        $this->assign("data", $data);
        $this->display();
    }

    // function ajaxshow(){
    //     sleep(1);
    //     $db = K("arc");
    //     $total = $db->where("huishou=0")->count(); //获得总条数
    //     $page=new page($total,"10",3,1);
    //     $data=$db->where("huishou=0")->order("aid asc")->limit($page->limit())->all();
    //     $page=$page->show();
    //     // $this->assign("page",$page);
    //     // $this->assign("data", $data);
    //     echo json_encode(array('num' => 1, 'arr' => $data));
    //     die;
    // }

    //添加文章视图
    function addshow() {
        //如果没有选择添加栏目，获得所有栏目
        C("debug",0);
        if (!isset($_GET['cid'])) {
            $this->assignCategoryData();
        }else{
            $this->assignCategoryData();
            $this->assign("lid", $_GET['cid']);
        }
        $flag=M("flag");
        $flag=$flag->all();
        $this->assign("flag",$flag);
        $this->assign("url",$_SERVER["HTTP_REFERER"]);
        $this->display();
    }

    function add() {
        $data = $_POST;
        // p($data);die;
        $flag = $data['flag'];
        // $name=$_SESSION['easyuser'];
        // $u=m("user");
        // $u=$u->where("username=".$name)->find();
        $data['shenhe'] = 1;
        $data['addtime'] = time();
        $data['thumbimg'] = isset($data['thumbimg'][1]) ? $data['thumbimg'][1]['path'] : ''; //缩略图
        $db = M("arc");
        if ($insertId=$db->add($data)) {
            foreach ($flag as $f) {
                $arc_flag = array(
                    "flag_fid" => $f,
                    "arc_aid" => $insertId
                );
                $db->table("arc_flag")->add($arc_flag);
            }
            go("index");
        } else {
            $this->error("文章添加失败,请重试");
        }
    }

    //修改文章显示界面
    function editshow() {
        // C("debug",0);
        // var_dump(C("huishou"));die;
        if ($aid = $this->_get("aid")) {
            $this->assignCategoryData(); //分配栏目数据
            $db = M('arc');

            $field = $db->find($aid); //获得编辑文章的原数据
            if($field['thumbimg']!=""){
               $thumbimg = array(
                    array(
                        "alt"=>"abc",
                        "path" => $field['thumbimg'],
                        // "thumb" => array($field['thumbimg'])
                    )
               );
            }else{
                // $flag=M("flag");
                // $flag=$flag->all();
                $thumbimg ='';
            }
            $flag=M("flag");
            $flag=$flag->all();
            $arc_flag=M("arc_flag");
            $arc_flag=$arc_flag->where("arc_aid=".$aid)->all();
            $a=array(); 
            $b=array(); 
            foreach ($arc_flag as $v) {
                 array_push($a, $v['flag_fid']);
                 array_push($b, $v['arc_aid']);
             } 
            foreach ($flag as $k => $v) {
                if (in_array($v['fid'], $a)) {
                    $flag[$k]['close']=1;
                }else{
                    $flag[$k]['close']=0; 
                }
            } 
             // p($flag);die; 
            $this->assign("b",$b);
            $this->assign("flag",$flag);
            $this->assign("thumbimg", $thumbimg);  
            // $this->assign("url",$_SERVER["HTTP_REFERER"]);
            $this->assign("field", $field);
            $this->display();
        }
    }

    //删除缩略图
    function delthumb() {
        $aid = $this->_get("aid");
        if ($aid) {
            $_POST=array("aid"=>$aid,'thumbimg'=>"");
            $db = M("arc");
            if($db->update()){
                $data['stat'] = "success";
                $data['msg'] = "";
            }else{
                $data['stat'] = "error";
                $data['msg'] = "删除缩略图失败";
            }
            
            echo json_encode($data);exit;
        }
    }

    //修改文章记录
    function save() {
        // p($_POST);die;
        $data = $_POST;
        $flag = $data['flag'];
        $data['thumbimg'] = isset($data['thumbimg'][1]['path']) ? $data['thumbimg'][1]['path'] : ''; //缩略图
        $db = M("arc");
        if ($db->save($data) >= 0) {
            $db->table("arc_flag")->where("arc_aid=".$data['aid'])->del();
            if (!empty($flag)) {
                foreach ($flag as $f) {
                    $arc_flag = array(
                        "flag_fid" => $f,
                        "arc_aid" => $data['aid']
                    );
                    $db->table("arc_flag")->add($arc_flag);
                }
            }
            $this->success("文章修改成功", "index");
        } else {
            $this->error("编辑文章失败，请重试");
        }
    }


    //删除文章
    function delarc() {
        $aid = $this->_get("aid");
        // $tem=C("huihou");
        // var_dump(C("huishou"));die;
        if ($aid) {
            $db = M("arc");
            $data = array(); //返回客户端的数据
            if ($db->find("aid=" . $aid)) {
                if (C("huishou")) {
                    $_POST=array("aid"=>$aid,'huishou'=>1);
                    $db->update();
                }else{
                    $db->del($aid);  
                }
                $data['stat'] = "success";
                $data['msg'] = "";
            } else {
                $data['stat'] = "error";
                $data['msg'] = "删除文章失败,请重试";
            }
            echo json_encode($data);exit;
        }
    }

    //审核文章
    function shenhearc() {
        $aid = $this->_get("aid");
        // $tem=C("huihou");
        // var_dump(C("huishou"));die;
        if ($aid) {
            $db = M("arc");
            $data = array(); //返回客户端的数据
            if ($db->find("aid=" . $aid)) {
                $_POST=array("aid"=>$aid,'shenhe'=>1);
                $db->update();
                $data['stat'] = "success";
                $data['msg'] = "";
            } else {
                $data['stat'] = "error";
                $data['msg'] = "删除文章失败,请重试";
            }
            echo json_encode($data);exit;
        }
    }


    //取消审核文章
    function quxiaoshenhearc() {
        $aid = $this->_get("aid");
        // $tem=C("huihou");
        // var_dump(C("huishou"));die;
        if ($aid) {
            $db = M("arc");
            $data = array(); //返回客户端的数据
            if ($db->find("aid=" . $aid)) {
                $_POST=array("aid"=>$aid,'shenhe'=>0);
                $db->update();
                $data['stat'] = "success";
                $data['msg'] = "";
            } else {
                $data['stat'] = "error";
                $data['msg'] = "删除文章失败,请重试";
            }
            echo json_encode($data);exit;
        }
    }


    //底部删除按钮
    function delall() {
        $c=isset($_GET['aid'])?1:0;
        $aid = $this->_get("aid");
        $aid = explode(',', $aid);
        // $data['msg'] = var_dump($aid);
        // echo json_encode($data);exit;
        if ($c) {
            // $data['msg'] = $aid;
            // echo json_encode($data);exit;
            $data = array(); //返回客户端的数据
            foreach ($aid as $v) {
                $db = M("arc");
                if ($db->find("aid=" . $v)) {
                    if (C("huishou")) {
                        $_POST=array("aid"=>$v,'huishou'=>1);
                        $db->update();
                    }else{
                        $db->del($v);  
                    }
                    // $db->del($v);
                    $data['stat'] = "success";
                    $data['msg'] = "";
                }else {
                    $data['stat'] = "error";
                    $data['msg'] = "删除文章失败,请重试";
                }
            }
            echo json_encode($data);exit;
        }
    }


    /**
     * 向模板分配置栏目
     * @access  private
     */
    private function assignCategoryData() {
        $db = M("category");
        $result = $db->all();
        $data = Data::channel($result, "cid", "pid", 0, "", 2, "--");
        $this->assign("category", $data);
    }


    function gouwuche(){
       $data = array(
         "id" => 4, //商品ID
         "name"=>"后盾网PHP视频教程光盘",//商品名称
         "num" => 3, //商品数量
         "price" => 100, //商品价格
         "options" => array(//其他参数如价格、颜色、可以为数组或字符串
                 "color" => "red",
                 "size" => "L"
                  )
       );
      cart::add($data); //添加到购物车
      // p($_SESSION);


      $data=array(
         "sid"=>"05749ed3",//唯一sid，添加购物车时自动生成
         "num"=>50
      );
      Cart::update($data);

      p($_SESSION);
    }

}

?>
